package com.lyj.io.module.sys.controller;

import com.lyj.io.annotation.SysLogAnnotation;
import com.lyj.io.common.utils.PageUtils;
import com.lyj.io.common.utils.R;
import com.lyj.io.common.dto.IdsDto;
import com.lyj.io.module.sys.entity.SysOrg;
import com.lyj.io.module.sys.service.SysOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 部门相关api
 * @author:liyajie
 * @createTime:2021/12/17 15:42
 * @version:1.0
 */
@Api(value = "SysOrgController", tags = "部门相关api")
@RestController
@RequestMapping("/api/v1/org")
public class SysOrgController {

    @Autowired
    SysOrgService sysOrgService;

    /**
     * 查询部门列表
     * @param
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("查询部门列表")
    @ApiOperation(value = "查询部门列表", notes = "查询部门列表")
    @PostMapping("/getOrgList")
    public R getOrgList(@RequestBody Map<String, Object> params) {
        PageUtils page = sysOrgService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 查询子部门
     * @param
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("查询子部门")
    @ApiOperation(value = "查询子部门", notes = "查询子部门")
    @GetMapping("/getChildrenOrg")
    public R getChildrenOrg(@RequestParam Map<String, Object> params) {
        List<SysOrg> orgList = sysOrgService.getChildrenOrg(String.valueOf(params.get("parentOrg")));
        return R.ok().put("data", orgList);
    }

    /**
     * 新增部门
     * @param
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("新增部门")
    @ApiOperation(value = "新增部门", notes = "新增部门")
    @PostMapping("/addOrg")
    public R addOrg(@RequestBody SysOrg sysOrg) {
        sysOrgService.save(sysOrg);
        return R.ok();
    }

    /**
     * 删除部门
     * @param
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("删除部门")
    @ApiOperation(value = "删除部门", notes = "删除部门")
    @PostMapping("/delOrg")
    public R delOrg(@RequestBody IdsDto idsDto) {
        String ids = idsDto.getIds();
        sysOrgService.removeByIds(Arrays.asList(ids.split(",")));
        return R.ok();
    }

    /**
     * 修改部门
     * @param sysOrg
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/10/8 15:55
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("修改部门")
    @ApiOperation(value = "修改部门", notes = "修改部门")
    @PostMapping("/updateOrg")
    public R update(@RequestBody SysOrg sysOrg) {
        sysOrgService.updateById(sysOrg);
        return R.ok();
    }

    /**
     * 修改部门状态
     * @param sysOrg
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/10/8 15:55
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("修改部门状态")
    @ApiOperation(value = "修改部门状态", notes = "修改部门状态")
    @PostMapping("/changeOrgStatus")
    public R changeOrgStatus(@RequestBody SysOrg sysOrg) {
        sysOrgService.changeOrgStatus(sysOrg);
        return R.ok();
    }
}
